import {
  getUserInfo,
  getMyAsk,
  getMyAnswer,
  delMyAsk,
  evaluate,
} from "@/static/js/quiz.js";

export default {
  data() {
    return {
      activeStyle: {
        color: "#1C87F3",
        fontWeight: "bold",
        fontSize: "36rpx",
        transform: "scale(1.05)",
        whiteSpace: "nowrap",
      },
      inactiveStyle: {},
      userInfo: {
        color: "#666666",
        fontSize: "30rpx",
        transform: "scale(1)",
      },
      navList: [
        {
          id: 1,
          name: "我的提问",
          type: 1,
          disabled: false,
        },
        {
          id: 2,
          name: "我的回答",
          type: 2,
          disabled: false,
        },
      ],
      askList: [],
      listStatus: "noMore",
      askShow: true,
      current: 0,
      answerList: [],
      listTouchStart: 0,
      listTouchDirection: null,
      modalName: null,
      delShow: false,
      delId: null,
      requestData: {
        page: 1,
        pagesize: 10,
      },
      isRefreshing: false, //界面下拉的状态
      evaluateShow: false, //评价弹框
      count: 5, //评价星星数
      starValue: 0, //星星值
      inputTxt: "", //输入框中的评价
      label: "", //评价标签名
      evaluateQuizData: {}, //评价的问题数据
      itemTxtList1: [
        {
          name: "没解决问题",
          checked: false,
        },
        {
          name: "不专业",
          checked: false,
        },
        {
          name: "态度很差",
          checked: false,
        },
        {
          name: "回复太慢",
          checked: false,
        },
        {
          name: "听不懂回答",
          checked: false,
        },
        {
          name: "回答敷衍",
          checked: false,
        },
      ],
      itemTxtList2: [
        {
          name: "回复太慢",
          checked: false,
        },
        {
          name: "回答敷衍",
          checked: false,
        },
        {
          name: "听不懂回答",
          checked: false,
        },
        {
          name: "不够专业",
          checked: false,
        },
        {
          name: "听不懂回答",
          checked: false,
        },
        {
          name: "没解决问题",
          checked: false,
        },
      ],
      itemTxtList3: [
        {
          name: "态度一般",
          checked: false,
        },
        {
          name: "回答不够认真",
          checked: false,
        },
        {
          name: "不够专业",
          checked: false,
        },
        {
          name: "回答不够清楚",
          checked: false,
        },
        {
          name: "回答较慢",
          checked: false,
        },
        {
          name: "解决部分问题",
          checked: false,
        },
      ],
      itemTxtList4: [
        {
          name: "态度热情",
          checked: false,
        },
        {
          name: "回复很及时",
          checked: false,
        },
        {
          name: "专业度高",
          checked: false,
        },
        {
          name: "解决了问题",
          checked: false,
        },
        {
          name: "回答很认真",
          checked: false,
        },
        {
          name: "回答很清楚",
          checked: false,
        },
      ],
      itemTxtList5: [
        {
          name: "态度非常好",
          checked: false,
        },
        {
          name: "回答迅速",
          checked: false,
        },
        {
          name: "回答很专业",
          checked: false,
        },
        {
          name: "解决了问题",
          checked: false,
        },
        {
          name: "回答很认真",
          checked: false,
        },
        {
          name: "回答很清楚",
          checked: false,
        },
      ],
    };
  },
  onLoad(e) {
    this.getUserInformation();
    this.getAskLists();
  },
  onShow() {
    this.getUserInformation();
    this.getAskLists();
  },

  methods: {
    //获取用户信息
    getUserInformation() {
      getUserInfo().then((res) => {
        if (res.code == 1) {
          this.userInfo = res.data;
          if (res.data.is_major == 1) {
            this.navList[1].disabled = false;
          } else {
            this.navList[1].disabled = true;
          }
        }
      });
    },

    //我的提问列表
    async getAskLists() {
      this.listStatus = "loading";
      getMyAsk(this.requestData).then((res) => {
        if (res.code == 1) {
          let arr = [];
          if (this.requestData.page == 1) {
            this.askList = [];
          }
          let newList = res.data.list;
          if (newList.length >= this.requestData.pagesize) {
            this.listStatus = "more";
          } else {
            this.listStatus = "noMore";
          }
          let arr1 = arr.concat(newList);
          arr1.forEach((val) => {
            let obj = {
              title: val.title,
              quiz_nickname: val.quiz_nickname,
              add_time_text: val.add_time_text,
              quiz_avatar: val.quiz_avatar,
              id: val.id,
              status: val.status,
              is_evaluate: val.is_evaluate,
            };
            this.askList.push(obj);
          });
          this.isRefreshing = false;
        }
      });
    },

    //前往回答会话页面
    goChatRoom(item) {
      if (item.status == 3) {
        uni.navigateTo({
          url: "/quiz/askInfo/index?id=" + item.id,
        });
      } else {
        uni.navigateTo({
          url: "/quiz/chatRoom/index?id=" + item.id,
        });
      }
    },
    goAddAnswer(item) {
      uni.navigateTo({
        url: "/quiz/chatRoom/index?type=1&id=" + item.id,
      });
    },

    //下拉刷新
    reload() {
      this.isRefreshing = true;
      this.requestData.page = 1;
      this.getAskLists();
      this.getMyAnswerList();
    },

    loadMore() {
      if (this.listStatus == "more") {
        this.requestData.page++;
        this.getAskLists();
        this.getMyAnswerList();
      }
    },

    //获取我的回答列表
    async getMyAnswerList() {
      this.listStatus = "loading";
      getMyAnswer(this.requestData).then((res) => {
        if (res.code == 1) {
          let arr = [];
          if (this.requestData.page == 1) {
            this.answerList = [];
          }
          let newList = res.data.list;
          if (newList.length >= this.requestData.pagesize) {
            this.listStatus = "more";
          } else {
            this.listStatus = "noMore";
          }
          let arr1 = arr.concat(newList);
          arr1.forEach((val) => {
            let obj = {
              answer: JSON.parse(val.first_answer.message),
              name: val.quiz_nickname,
              avatar: val.quiz_avatar,
              time: val.add_time_text,
              status: val.status,
              content: val.title,
              id: val.id,
            };
            this.answerList.push(obj);
          });
          this.isRefreshing = false;
        }
      });
    },

    //删除待回答的问题
    async delQuiz(item) {
      if (item.status == 1) {
        this.delShow = true;
        this.delId = item.id;
      } else {
        this.$tools.msg("只有待回答的问题才可以删除哟~");
        return;
      }
    },

    //确认取消收藏
    async confirmDelLike() {
      let params = {
        id: this.delId,
      };
      delMyAsk(params).then((res) => {
        if (res.code == 1) {
          this.$tools.msg("删除成功");
          this.getAskLists();
        }
      });
      this.delShow = false;
    },

    clickMenu(e) {
      let that = this;
      if (e.type == 1) {
        this.askShow = true;
        this.getAskLists();
      } else if (e.type == 2 && e.disabled == true) {
        uni.showModal({
          title: "温馨提示",
          content: "成为专业人士才可以回答问题哦~",
          confirmText: "去成为",
          confirmColor: "#1C87F3",
          success: function (res) {
            if (res.confirm) {
              that.$tools.msg("敬请期待");
              // uni.navigateTo({
              //   url: "/pages/my/major/index",
              // });
            }
          },
        });
      } else {
        this.requestData.page = 1;
        this.askShow = false;
        this.getMyAnswerList();
      }
    },

    //返回上一层
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    // ListTouch触摸开始
    ListTouchStart(e) {
      this.listTouchStart = e.touches[0].pageX;
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.listTouchDirection =
        e.touches[0].pageX - this.listTouchStart > 0 ? "right" : "left";
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.listTouchDirection == "left") {
        this.modalName = e.currentTarget.dataset.target;
      } else {
        this.modalName = null;
      }
      this.listTouchDirection = null;
    },

    //去提问
    toQuickAsk() {
      uni.navigateTo({
        url: "/quiz/ask/index",
      });
    },

    //提交评价
    async evaluate() {
      var params = {
        quiz_id: this.evaluateQuizData.id,
        star: this.starValue,
        label: this.label ? this.label : "",
        evaluate: this.inputTxt,
      };
      var res = await evaluate(params);
      if (res.code == 1) {
        this.evaluateShow = false;
        this.$tools.msg("评价成功");
        setTimeout(() => {
          this.getAskLists();
        }, 2000);
      }
    },

    // 根据星星数量显示字符
    showTitle(starValue) {
      //总评价
      var text = "";
      switch (starValue) {
        case 1:
          text = "不满意，各方面都很差";
          break;
        case 2:
          text = "不满意，待提高";
          break;
        case 3:
          text = "一般般，仍可改善";
          break;
        case 4:
          text = "比较满意，仍可改善";
          break;
        case 5:
          text = "特别棒，无可挑剔";
          break;
      }
      return text;
    },

    //选择评论项
    chooseItem(item, index) {
      this.label = item.name;
      this.currentItem = index;
      if (this.currentItem == index) {
        item.checked = !item.checked;
      }
    },
    showEvaluate(e) {
      this.evaluateShow = true;
      this.evaluateQuizData = e;
    },
  },
};